define(function () {
    'use strict';

    function ctrl($scope,$filter,$ionicPopup,$q,$timeout,$ionicScrollDelegate,$stateParams,
                  CommonService,LocalCacheService,InfoService,WebService,$ionicLoading,ConcernService) {

        $scope.channels = {};
        $scope.vm = {};

        $scope.$on('$ionicView.beforeLeave', function() {
            if($scope.myPopup){
                $scope.myPopup.close();
            }

        });
        init();
        $scope.doRefresh = function() {
            init(true);
            $scope.$broadcast('scroll.refreshComplete');
        };

        $scope.$watch("vm.activeTab", function(newValue, oldValue){
            if(newValue) {
                init(true);
                $timeout(function(){
                    $ionicScrollDelegate.resize();
                }, 500);
            }
        });

        $scope.operUserCare = function(prod_code){
            ConcernService.concern(prod_code);
        }

        $scope.goTop = function(){
            $ionicScrollDelegate.scrollTop(true);
        }

        function init(refresh){
            if(!$scope.channels){
                $ionicLoading.show();
            }
            $scope.id = $stateParams['id'];
            $scope.index = $stateParams['index'];
            return showChannelImg(refresh);
        }

        function showChannelImg(refresh){

            if(refresh) {
                return InfoService.getChannelInfo($scope.vm.activeTab).then(function(data){
                    for(var i = 0; i < $scope.channels.length; i++) {
                        if($scope.channels[i].id == data.id) {
                            $scope.channels[i] = data;
                            $scope.channels[i].page_no = 1;
                            $scope.channels[i].products = new Array();
                            break;
                        }
                    }
                    $scope.vm.loadMore = true;
                    $scope.loadMore(refresh);
                });
            } else {
                return InfoService.getChannelInfosByParentId($scope.id).then(function(data){
                    $scope.channels = data;
                    for(var i = 0; i < $scope.channels.length; i++) {
                        var channel = $scope.channels[i];
                        if(i == 0 && $scope.index==1) {
                            $scope.vm.activeTab=channel.id;
                        }else if(i==1 && $scope.index==2){
                            $scope.vm.activeTab=channel.id;
                        }
                        channel.page_no = 1;
                        channel.products = new Array();
                        $ionicLoading.hide();
                    }
                    $scope.vm.loadMore = true;
                });
            }
        }

        $scope.loadMore = function(refresh) {
            for(var i = 0; i < $scope.channels.length; i++) {
                var channel = $scope.channels[i];
                if($scope.vm.activeTab == channel.id) {
                    var query;
                    if(refresh) {
                        channel.page_no = 1;
                        channel.products = new Array();
                    }
                    if(channel.catalog_mark == "otcfdsy") {
                        query = InfoService.getOtcProductsByChannelId(channel.id, channel.page_no,1);
                    } else {
                        query = InfoService.getProductsByChannelId(channel.id, channel.page_no,1);
                    }
                    query.then(function(products) {

                        if(products && products.length > 0) {
                            for(var i=0;i<products.length;i++){
                                var fund = products[i];
                                fund.prod_min_subscribe = $scope.index==1? $filter('prodMinSubscribe')(fund.prod_min_share):$filter('prodMinSubscribe')(CommonService.getMinAmount(fund));
                                if(channel.catalog_mark == "otcfdsy") {
                                    if("0" == fund.otc_status){
                                        var days = CommonService.stringToDate(fund.ipo_begin_date).getTime() -new Date().getTime();
                                        fund.bengin_date_show = Math.round(days / (1000 * 60 * 60 * 24));
                                    }
                                    fund.sell_status = CommonService.getOtcSellStatus(fund.prod_status, fund.otc_status);
                                }
                                if($scope.index) {
                                    fund.concern =  ConcernService.isConcern(fund.prod_code);
                                }
                            }
                            channel.products = channel.products.concat(products);
                            channel.page_no ++;
                            $scope.vm.page_no = channel.page_no;
                        } else {
                            $scope.vm.loadMore = false;
                        }

                        $scope.$broadcast('scroll.infiniteScrollComplete');
                    });
                    return;
                }
            }
        };

        $scope.showAds = showAds;

        function showAds(index, type){

            var ad = "xcm_tfTeam_" + (index+1);
            WebService.wtAd(ad).then(function(data){
                if(data) {
                    $scope.ads = data;
                    var adKey = "ad_" + ad;
                    if((type || !LocalCacheService.get(adKey)) && $scope.ads.length > 0) {
                         var myPopup = $scope.myPopup = $ionicPopup.show({
                            cssClass: "kDialog",
                            templateUrl: "product/ads.html",
                            scope: $scope,
                            buttons: [
                                { text: '确定' }
                            ]
                        });
                        LocalCacheService.set(adKey, true);
                    }
                }
            });
        };

    }

    ctrl.$inject = ['$scope','$filter','$ionicPopup','$q','$timeout','$ionicScrollDelegate','$stateParams',
        'CommonService','LocalCacheService','InfoService','WebService','$ionicLoading','ConcernService'];
    return ctrl;
});
